"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库time数据类型
Case Name   : time类型，输入格式为 '[-][D] hh:mm:ss[.frac]'，time精度测试，
              部分测试点合理报错
Create At   : 2023.08.17
Owner       : z@wan005
Description :
    1、time类型精度测试
    2、清理环境
Expect      :
    1、不带精度成功；精度0-6成功；精度超过6有warning提示并返回最大精度为6的值，精度为
    负数及中文报错，括号内无精度值报错
    2、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest29(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_time_0029"
        self.tb_name = "tb_time_0029"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '-step1:time类型精度测试;expect:不带精度成功；精度0-6成功；精度超过6，' \
               '有warning提示并返回最大精度为6的值，精度为负数及中文报错，括号内无精度值报错'
        self.log.info(text)
        sql_cmd = f"select ('12:12:12.123456')::time;" \
                  f"select ('12:12:12.123456')::time(0);" \
                  f"select ('12:12:12.56')::time(0);" \
                  f"select ('12:12:12.123456')::time(1);" \
                  f"select ('12:12:12.123456')::time(2);" \
                  f"select ('12:12:12.123456')::time(3);" \
                  f"select ('12:12:12.123456')::time(4);" \
                  f"select ('12:12:12.123456')::time(5);" \
                  f"select ('12:12:12.123456')::time(6);" \
                  f"select ('12:12:12.1234567')::time(7);" \
                  f"select ('12:12:12.1234567')::time(-1);" \
                  f"select ('12:12:12.1234567')::time(5.5);" \
                  f"select ('12:12:12.1234567')::time();" \
                  f"select ('12:12:12.1234567')::time('精度');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(
            'WARNING:  TIME(7) precision reduced to maximum allowed, 6',
            sql_res, '执行失败' + text)
        self.assertIn('ERROR:  TIME(-1) precision must not be negative',
                      sql_res, '执行失败' + text)
        self.assertEqual(2, sql_res.count('ERROR:  Un-support feature'),
                         '执行失败' + text)
        self.assertIn('ERROR:  syntax error at or near ")"', sql_res,
                      '执行失败' + text)
        res_list = ['12:12:12', '12:12:12', '12:12:13', '12:12:12.1',
                    '12:12:12.12', '12:12:12.123', '12:12:12.1235',
                    '12:12:12.12346', '12:12:12.123456', '12:12:12.123457']
        for res in res_list:
            self.assertIn(res, sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '----step2:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
